<template>
  <el-dialog :model-value="show" title="分配角色" @close="emit('close')">
    <el-form :model="form" ref="formRef" :rules="rules">
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-input v-model="form.nickName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          autocomplete="off"
          :disabled="id !== 0"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="form.note"
          :rows="4"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitFn"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { reactive, ref, watch } from "vue";
import { addUserApi, editUserApi, getUserByIdApi } from "./api/api";
const emit = defineEmits(["close"]);
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  id: {
    type: Number,
    default: 0,
  },
});
watch(
  () => props.id,
  () => {
    if (props.id !== 0) {
      getUserByIdApi(props.id).then((res) => {
        if (res.code === 200) {
          Object.assign(form, res.data);
        }
      });
    }
  }
);
const form = reactive<List>({
  username: "",
  password: "",
  email: "",
  nickName: "",
  status: 0,
});
// 验证邮箱
const validateEmail = (rule: any, value: string, callback: any) => {
  if (!/^\w+[@]\w+[.]\w+/.test(value)) {
    return callback(new Error("邮箱格式错误"));
  } else {
    callback();
  }
};
// 校验规则
const rules = {
  username: [{ required: true, trigger: "blur", message: "请填写用户名" }],
  email: [{ validator: validateEmail, trigger: "blur" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }],
  nickName: [{ required: true, trigger: "blur", message: "请填写昵称" }],
};
// 取消
const cancel = () => {
  emit("close", true);
};
const formRef = ref<FormInstance>();

// 确认添加
const addUser = () => {
  ElMessageBox.confirm("确定要添加用户吗？", "添加用户", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
  })
    .then(() => {
      // 发请求
      addUserApi(form).then((res) => {
        if (res.code === 200) {
          // 提示
          ElMessage.success("添加成功");
          // 把dialog关闭
          // 刷新数据
          emit("close", true);
          // 清空表单
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {
      console.log("点击取消");
    });
};
// 确认编辑
const editUser = () => {
  ElMessageBox.confirm("确定要修改当前用户的信息吗？", "修改用户信息", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    editUserApi(form).then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
        formRef.value?.resetFields();
        emit("close", true);
      }
    });
  });
};
// 确认按钮
const submitFn = () => {
  // 再做一次数据的校验
  formRef.value?.validate((ok: boolean) => {
    if (ok) {
      if (props.id === 0) {
        addUser();
      } else {
        editUser();
      }
      //  如果是添加
    } else {
      // 提示
      ElMessage.error("添加失败");
    }
  });
};
</script>
<style lang="less" scoped></style>
